//
//  MANMainViewController.m
//  Three
//
//  Created by 夜猫子 on 2017/3/22.
//  Copyright © 2017年 夜猫子. All rights reserved.
//

#import "MANMainViewController.h"
#import "MANNavigationController.h"
#import "CZAdditions.h"
#import "MANSliderViewController.h"

@interface MANMainViewController ()

@end

@implementation MANMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加子控制器
    [self addControllers];
}


/**
 添加子控制器
 */
-(void)addControllers{
    
    
    UIViewController *vc1 = [self controllerWithClassName:@"MANHomeViewController" andTitle:@"首页" andImgName:@"tab_normal_1"];
    
    UIViewController *vc2 = [self controllerWithClassName:@"MANDisCoverViewController" andTitle:@"消息" andImgName:@"tab_normal_2"];
    
    UIViewController *vc3 = [self controllerWithClassName:@"MANHealthViewController" andTitle:@"健康" andImgName:@"tab_normal_3"];
    self.tabBar.tintColor = [UIColor whiteColor];
    
    self.viewControllers = @[ vc1,vc2,vc3 ];
}


/**
 传入的参数创建控制器
 */
-(UIViewController *)controllerWithClassName:(NSString *)clsName andTitle:(NSString *)title andImgName:(NSString *)imgName{
    
    Class cls = NSClassFromString(clsName);
    //下断言：
    NSAssert( [cls isSubclassOfClass:[UIViewController class]]  , @"传入的类名不是合法的控制器类名");
    
    //父类指针可以指向任意的子类对象
    UIViewController *vc = [cls new];
   
    
    vc.title = title;
//    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:78 green:194 blue:156 alpha:1]}forState:UIControlStateNormal];
    [vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor cz_colorWithHex:0x20c6c6]}forState:UIControlStateNormal];
    //设置tabBar的标签图像
    UIImage *normalimg = [UIImage imageNamed:imgName];
    UIImage *selectimg = [UIImage imageNamed:[imgName stringByAppendingString:@"_selected"]];
    vc.tabBarItem.image = [normalimg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    vc.tabBarItem.selectedImage = [selectimg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    [self setBtn:vc];
    
    //包装navigationController
    MANNavigationController *nav = [[MANNavigationController alloc] initWithRootViewController:vc];
    
    return nav;
}


/**
 导航栏左上角设置按钮

 @param vc 控制器
 */
- (void)setBtn:(UIViewController *)vc {
    
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
    
    [btn setBackgroundImage:[UIImage imageNamed:@"menusnew"] forState:UIControlStateNormal];
    
    vc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    [btn addTarget:self action:@selector(leftBtn) forControlEvents:UIControlEventTouchUpInside];
    
}


/**
 导航栏左上角的点击事件
 */
- (void)leftBtn {
    
//    MANSliderViewController *sliderVC = [[MANSliderViewController alloc]init];
//    
//    [sliderVC showLeftWithWidth:sliderVC.view.bounds.size.width - 64];
    
}


@end
